package com.jxau.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jxau.controller.utils.Result;
import com.jxau.domain.Comment;
import com.jxau.service.CommentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/MoviesWebsite/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    //获取所有评论
    @GetMapping
    public Result getAll(){
        return new Result(true, commentService.list());
    }

    //添加评论
    @PostMapping
    public Result save(@RequestBody Comment comment) {
        return new Result(commentService.save(comment));
    }

    //修改评论
    @PutMapping
    public Result modify(@RequestBody Comment comment){
        return new Result(commentService.updateById(comment));
    }

    //删除评论
    @DeleteMapping("{commentid}")
    public Result delete(@PathVariable Integer commentid){
        return new Result(commentService.removeById(commentid));
    }

    //按评论id查询评论
    @GetMapping("{commentid}")
    public Result getById(@PathVariable Integer commentid){
        Comment comment =commentService.getById(commentid);
        if(comment != null){
            return new Result(true, comment);
        }else{
            return new Result(false, null);
        }
    }

    //分页查询评论
    @GetMapping("{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize, Comment comment){
        IPage<Comment> page = commentService.getPage(currentPage, pageSize, comment);
        if(currentPage > page.getPages()){
            page = commentService.getPage((int)page.getPages(), pageSize, comment);
        }
        return new Result(true, page);
    }
}
